package org.hackystat.projectbrowser.page.setaction;

import java.util.ArrayList;
import java.util.List;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.hackystat.projectbrowser.ProjectBrowserSession;
import org.hackystat.ambientdevice.server.jaxb.Project;
import org.hackystat.ambientdevice.server.jaxb.Action;
import org.hackystat.ambientdevice.server.jaxb.TriggerActionPair;

/**
 * The class list view that lists all the actions. 
 * @author Edward Kim
 *
 */
public class ChooseActionTable extends ListView {

  /** Supports serialization. */
  private static final long serialVersionUID = 1L;

  private List<TriggerActionPair> pairs = new ArrayList<TriggerActionPair>();
  /** Displays text at the bottom of the screen for user information. */
  protected String footerFeedbackk = "";
  private Project project;
  private String tapName;

  /**
   * The table that list all actions.
   * @param id A string to be passed on to super (not needed). 
   * @param param The parameters of the page. 
   */
  public ChooseActionTable(String id, PageParameters param) {
    super(id);

    String projectName = param.getStringValue("projectName").toString();
    project = ProjectBrowserSession.get().getProjectTypeXMLByNameId(projectName);
    tapName = param.getStringValue("tapName").toString();

    List<Action> actions = new ArrayList<Action>();
    pairs = project.getTriggerActionPairs().getTriggerActionPair();

    for (TriggerActionPair pair : pairs) {
      if (pair.getId().equals(tapName)) {
        if (pair.getNegativeActions() == null && pair.getPositiveActions() == null) {
          System.out.println("There are not Trigger/Action Pairs.");
        }
        else if (pair.getPositiveActions() == null && pair.getNegativeActions() != null) {
          actions.addAll(pair.getNegativeActions().getAction());
        }

        else if (pair.getNegativeActions() == null && pair.getPositiveActions() != null) {
          actions.addAll(pair.getPositiveActions().getAction());
        }
        else {
          actions.addAll(pair.getNegativeActions().getAction());
          actions.addAll(pair.getPositiveActions().getAction());
        }
      }
    }

    setList(actions);
  }

  /**
   * Populates the table. 
   * @param item The items populated on the table. 
   */
  @Override
  protected void populateItem(ListItem item) {
    PageParameters param = new PageParameters();
    Action action = (Action) item.getModelObject();
    param.put("actionName", action.getId());
    item.add(new Label("actionName", action.getId()));
    // FOR NOW POINT TO THE SET TRIGGER PAGE BECAUSE WE HAVE NO PAGE TO POINT TO

    PageParameters param2 = new PageParameters();
    param2.put("projectName", project.getProjectName());
    item.add(new BookmarkablePageLink("projectLink", SetActionPage.class, param2));

    param2.put("tapName", tapName);
    //item.add(new BookmarkablePageLink("NewPositiveAction", NewPositiveActionName.class, param2));
    //item.add(new BookmarkablePageLink("NewNegativeAction", NewNegativeActionName.class, param2));
    param2.put("oldActionName", action.getId());
    item.add(new BookmarkablePageLink("EditActionName", EditActionName.class, param2));
    item.add(new DeleteActionLink
        ("deleteAction", action.getId(), project.getProjectName(), tapName));

  }

}
